﻿/*
Copyright (c) 2010 Wolfgang Bruessler

http://code.google.com/p/sporttracks-csv-export/

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ZoneFiveSoftware.Common.Data;
using ZoneFiveSoftware.Common.Data.Fitness;
using ZoneFiveSoftware.Common.Data.GPS;
using ZoneFiveSoftware.Common.Visuals;
using ZoneFiveSoftware.Common.Data.Measurement;
using ZoneFiveSoftware.Common.Visuals.Fitness;

namespace SportTracksCsvExporter
{
    public partial class ExportForm : Form
    {
        public List<Setting> profiles=null;
        public int selectedProfile = -1;
        public IActivity activity;
        public IList<IActivity> activities;

        public ExportForm()
        {
            InitializeComponent();
            CancelButton = buttonCancel;
            AcceptButton = buttonExport;
            activity = null;
        }

        private void ExportForm_Load(object sender, EventArgs e)
        {
            comboBoxProfile.Items.Clear();
            for (int i = 0; i < profiles.Count; i++)
            {
                comboBoxProfile.Items.Add(profiles[i].profileName);
            }
            if ((selectedProfile >= 0) && (selectedProfile < profiles.Count))
            {
                comboBoxProfile.SelectedIndex = selectedProfile;
            }
            else
            {
                comboBoxProfile.SelectedIndex = 0;
            }
            updateButtons();
            updateFilename();

        }

        private void updateFilename()
        {
            Setting profile = null;

            if ((profiles.Count > 0) && (comboBoxProfile.SelectedIndex >= 0) && (comboBoxProfile.SelectedIndex < profiles.Count))
            {
                profile=profiles[comboBoxProfile.SelectedIndex];
            }
            else
            {
                profile = new Setting();
            }

            if (radioButtonExportActivity.Checked)
            {
                if (radioButtonExportSelection.Checked)
                {
                    if (activity != null)
                    {
                        textBoxFilename.Text=ExportActivitiesToCsvAction.GenerateDefaultFilename(activity, profile);
                        textBoxFilename.Enabled = true;
                    }
                    else if ((activities != null) && (activities.Count > 0))
                    {
                        textBoxFilename.Text = ExportActivitiesToCsvAction.GenerateDefaultFilename(activities[0], profile);
                        textBoxFilename.Enabled = false;
                    }
                    else
                    {
                        textBoxFilename.Text = "";
                        textBoxFilename.Enabled = true;
                    }
                }
                else
                {
                    textBoxFilename.Text = ExportActivitiesToCsvAction.GenerateDefaultFilename(dateTimePickerFrom.Value, dateTimePickerTo.Value, profile);
                }
            }
            else
            {
                if (radioButtonExportSelection.Checked)
                {
                    if (activity != null)
                    {
                        textBoxFilename.Text = ExportActivitiesToCsvAction.GenerateDefaultFilename(activity, profile);
                        textBoxFilename.Enabled = true;
                    }
                    else if (activities != null)
                    {
                        textBoxFilename.Text = ExportActivitiesToCsvAction.GenerateDefaultFilename(activities, profile);
                        textBoxFilename.Enabled = true;
                    }
                    else
                    {
                        textBoxFilename.Text = "";
                        textBoxFilename.Enabled = true;
                    }
                }
                else
                {
                    textBoxFilename.Text = ExportActivitiesToCsvAction.GenerateDefaultFilename(dateTimePickerFrom.Value, dateTimePickerTo.Value, profile);
                }
            }
        }

        private void updateButtons()
        {
            if (radioButtonExportSelection.Checked)
            {
                dateTimePickerFrom.Enabled = false;
                dateTimePickerTo.Enabled = false;
                checkBoxExportJournal.Enabled = false;
            }
            else
            {
                dateTimePickerFrom.Enabled = true;
                dateTimePickerTo.Enabled = true;
                checkBoxExportJournal.Enabled = true;
            }

            if (comboBoxProfile.Items.Count > 1)
            {
                buttonDelete.Enabled = true;
            }
            else
            {
                buttonDelete.Enabled = false;
            }
        }

        private void linkLabelCopyright_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                linkLabelCopyright.LinkVisited = true;
                System.Diagnostics.Process.Start("http://www.bruessler.name/sporttracks");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void buttonExport_Click(object sender, EventArgs e)
        {
            selectedProfile = comboBoxProfile.SelectedIndex;
            DialogResult = DialogResult.OK;
            Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            int selectedIndex = comboBoxProfile.SelectedIndex;
            if ((selectedIndex >= 0) && (selectedIndex < profiles.Count))
            {
                EditProfileForm editProfileForm = new EditProfileForm();
                editProfileForm.s = profiles[selectedIndex];
                if (editProfileForm.ShowDialog() == DialogResult.OK)
                {
                    comboBoxProfile.Items.Clear();
                    for (int i = 0; i < profiles.Count; i++)
                    {
                        comboBoxProfile.Items.Add(profiles[i].profileName);
                    }
                    comboBoxProfile.SelectedIndex = selectedIndex;
                    updateFilename();
                }
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            AddProfile addProfile = new AddProfile();
            addProfile.radioButtonCopySelected.Checked = true;
            if (addProfile.ShowDialog() == DialogResult.OK)
            {
                EditProfileForm editProfileForm = new EditProfileForm();

                if (addProfile.radioButtonCopySelected.Checked == true)
                {
                    int selectedIndex = comboBoxProfile.SelectedIndex;
                    if ((selectedIndex >= 0) && (selectedIndex < profiles.Count))
                    {
                        editProfileForm.s = new Setting(profiles[selectedIndex]);
                    }
                    else
                    {
                        editProfileForm.s = new Setting();
                    }
                }
                else
                {
                    editProfileForm.s = new Setting();
                }

                if (editProfileForm.ShowDialog() == DialogResult.OK)
                {
                    profiles.Add(editProfileForm.s);
                    int index = comboBoxProfile.Items.Add(editProfileForm.s.profileName);
                    comboBoxProfile.SelectedIndex = index;
                    updateFilename();
                }
            }
            updateButtons();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if ((profiles.Count>1) && (comboBoxProfile.SelectedIndex >= 0) && (comboBoxProfile.SelectedIndex < profiles.Count))
            {
                profiles.RemoveAt(comboBoxProfile.SelectedIndex);
                comboBoxProfile.Items.RemoveAt(comboBoxProfile.SelectedIndex);
                comboBoxProfile.SelectedIndex = 0;
                updateFilename();
            }
            updateButtons();
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
            folderBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop;
            folderBrowserDialog.ShowNewFolderButton = true;
            folderBrowserDialog.SelectedPath = textBoxExportTo.Text;
            
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                textBoxExportTo.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void radioButtonExportSelection_CheckedChanged(object sender, EventArgs e)
        {
            updateButtons();
            updateFilename();
        }

        private void radioButtonExportActivity_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonExportActivity.Checked == true)
            {
                checkBoxExportJournal.Checked = false;
            }
            updateFilename();
        }

        private void checkBoxExportJournal_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxExportJournal.Checked == true)
            {
                radioButtonExportSummary.Checked = true;
            }
        }

        private void dateTimePickerFrom_ValueChanged(object sender, EventArgs e)
        {
            updateFilename();
        }

        private void dateTimePickerTo_ValueChanged(object sender, EventArgs e)
        {
            updateFilename();
        }
    }
}
